package com.cqhfan.common.utils;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class MD5Util {
    public static String encrypt(String text) {
        try {
            // 创建MD5加密对象
            MessageDigest md = MessageDigest.getInstance("MD5");
            // 更新消息摘要
            md.update(text.getBytes());
            // 获取加密后的字节数组
            byte[] byteData = md.digest();

            // 将字节数组转换为十六进制字符串
            StringBuilder sb = new StringBuilder();
            for (byte b : byteData) {
                sb.append(String.format("%02x", b));
            }
            return sb.toString();
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
            return null;
        }
    }

    public static void main(String[] args) {
        String text = "Sch531294123456";
        String encryptedText = encrypt(text);
        System.out.println("加密后的MD5值为：" + encryptedText);
    }
}
